package be.nvlooy.moviereminder.controller;

import java.util.List;

import be.nvlooy.moviereminder.service.ReleaseDateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import be.nvlooy.moviereminder.common.SessionMessage;
import be.nvlooy.moviereminder.dto.MovieDTO;
import be.nvlooy.moviereminder.entities.Collection;
import be.nvlooy.moviereminder.entities.Movie;
import be.nvlooy.moviereminder.entities.User;
import be.nvlooy.moviereminder.exception.ServiceException;
import be.nvlooy.moviereminder.service.CollectionService;
import be.nvlooy.moviereminder.service.MovieService;
import be.nvlooy.moviereminder.service.MovieSourceService;

@Controller
@RequestMapping(value="/findmovie")
public class FindMediaController extends BasicController {
	
	private MovieSourceService movieSourceService;
	private MovieService movieService;
	private CollectionService collectionService;
    private ReleaseDateService releaseDateService;
	
	@Autowired
	public void setTmdbService(MovieSourceService tmdbService) {
		this.movieSourceService = tmdbService;
	}
	
	@Autowired
	public void setMovieService(MovieService movieService) {
		this.movieService = movieService;
	}	
	
	@Autowired
	public void setCollectionService(CollectionService collectionService) {
		this.collectionService = collectionService;
	}

	@Autowired
	public void setReleaseDateService(ReleaseDateService releaseDateService) {
		this.releaseDateService = releaseDateService;
	}

	@RequestMapping(value="")
	public String index() {
		return "/findmovie/search";
	}
	
	@RequestMapping(value="/search")
	public String search() {
		return "/findmovie/search";
	}
	
	@RequestMapping(value="/search", params={"search"})
	public String search(@RequestParam("search") String search, Model model) {
		// search for movies with a given name on the Tmdb server
		List<MovieDTO> movies = movieSourceService.findMovieDTOByName(search);
		
		if(movies.size() == 1)
			return "redirect:info/"+movies.get(0).getSourceId();
		else {
			model.addAttribute("movies", movies);
			model.addAttribute("search", search);
			
			return "/findmovie/search";
		}
	}
	
	@RequestMapping(value="/info/{sourceId}")
	public String info(@PathVariable int sourceId, Model model) {
		MovieDTO movie = movieSourceService.getMovieDTOBySourceId(sourceId);
		model.addAttribute("movie", movie);
		return "/findmovie/info";
	}

	@RequestMapping(value="/addtocollection/{sourceId}")
	public String addMovieToCollection(@PathVariable int sourceId, Model model) {
		// first we ask the sourceService for a Movie POJO object from the given sourceId
		MovieDTO movieDto = movieSourceService.getMovieDTOBySourceId(sourceId);
		// add the movie first if needed
		Movie movie = movieService.addMovieFromDTO(movieDto);
		User user = getAuthenticatedUser();
		
		try {
			// add movie to the collection
			Collection collectionItem = collectionService.addMedia(user, movie);
			showFrontendMessage("collectionAddedSuccessfull", movie.getTitle());
			return "redirect:/collection/info/"+String.valueOf(collectionItem.getId());
		}
		catch(ServiceException e) {
			showFrontendMessage(e.getMessage(), movie.getTitle(), SessionMessage.MessageType.WARNING);
		}

		return "redirect:/collection";
	}

}
